<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="工序编号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="processNo">
              <a-input v-model="model.processNo" placeholder="请输入工序编号"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="工序名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="processName">
              <a-input v-model="model.processName" placeholder="请输入工序名称"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="计价类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="priceType">
              <j-dict-select-tag type="list" v-model="model.priceType" dictCode="mdm_process_price_type" placeholder="请选择计价类型" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="工作中心" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="workId">
              <j-search-select-tag v-model="model.workId" dict="mdm_work_center,name,id"  />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="标准工时" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="standardHours">
              <a-input-number v-model="model.standardHours" placeholder="请输入标准工时" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="准备工时" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="prepareHours">
              <a-input-number v-model="model.prepareHours" placeholder="请输入准备工时" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="等待工时" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="waitHours">
              <a-input-number v-model="model.waitHours" placeholder="请输入等待工时" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="单位" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="unit">
              <j-search-select-tag type="list" v-model="model.unit" dict="mdm_unit where unit_type = '08', unit_name,unit_code" placeholder="请选择单位" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="工价" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="empCost">
              <a-input-number v-model="model.empCost" placeholder="请输入工价" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="助记器" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="auxDesc">
              <a-input v-model="model.auxDesc" placeholder="请输入助记器"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="安排计划" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="planFlag">
              <j-switch v-model="model.planFlag" :options="[1,0]" ></j-switch>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="安排派工" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="dispatchFlag">
              <j-switch v-model="model.dispatchFlag" :options="[1,0]" ></j-switch>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="安排报工" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="reportFlag">
              <j-switch v-model="model.reportFlag" :options="[1,0]" ></j-switch>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="启用标志" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="enableFlag">
              <j-switch v-model="model.enableFlag" :options="[1,0]" ></j-switch>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="comm">
              <a-input v-model="model.comm" placeholder="请输入备注"  ></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'MdmProcessBaseForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
            priceType:"01",
            planFlag:"0",
            dispatchFlag:"0",
            reportFlag:"0",
            enableFlag:"1",
            unit:'12',
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
           processNo: [
              { required: true, message: '请输入工序编号!'},
           ],
           processName: [
              { required: true, message: '请输入工序名称!'},
           ],
        },
        url: {
          add: "/mdm/processBase/add",
          edit: "/mdm/processBase/edit",
          queryById: "/mdm/processBase/queryById"
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
         
        })
      },
    }
  }
</script>